class GuaScene {
    constructor(game) {
        this.game = game
        self = this
        this.elements = {}
    }

    // 每个 scene 元素集合 ｛"bullet"：[BulletA, BulletB], "player": [playerA, playerB]}
    addElement(element) {
        element.scene = this
        element.alive = element.alive || true

        var noName = 'noName'
        var elementName = element.name || noName

        if (!(elementName in this.elements)) {
            this.elements[elementName] = []
        }

        this.elements[elementName].push(element)
        // log("this.elements", this.elements)
    }

    getElementsByName(name) {
        var e = this.elements[name]
        return e
    }

    draw() {
        var names = Object.keys(this.elements)
        for (var i = 0; i < names.length; i++) {
            var name = names[i]
                // 死亡的元素清除掉
            this.elements[name] = this.elements[name].filter(e => e.alive)

            for (var j = 0; j < this.elements[name].length; j++) {
                var es = this.elements[name]
                var e = es[j]
                e.draw()
            }
        }
    }

    update() {
        if (this.debugModeEnabled && this.pause) {
            return
        }

        var names = Object.keys(this.elements)
        for (var i = 0; i < names.length; i++) {
            var name = names[i]
            for (var j = 0; j < this.elements[name].length; j++) {
                var e = this.elements[name][j]
                e.update()
            }
        }
        if (this.debugModeEnabled) {
            this.debug()
        }
    }
    debug() {


        var names = Object.keys(this.elements)
        for (var i = 0; i < names.length; i++) {
            var name = names[i]
            for (var j = 0; j < this.elements[name].length; j++) {
                var e = this.elements[name][j]
                e.debug && e.debug()
            }
        }

    }
}